#
# Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
#

# -*- mode: python; -*-
# src directory

import subprocess
import platform
Import('contrail_common_base_doc_files')
Import('contrail_common_io_doc_files')

subdirs = [
           'analytics/database',
           'bfd',
           'bgp',
           'db',
           'dns',
           'ifmap',
           'net',
           'route',
           'sandesh',
           'storage',
           'xml',
           'xmpp',
           'nodemgr',
           ]
 
if platform.system() != 'Darwin':
    subdirs += ['ksync']

if platform.system() != 'Windows':
    subdirs += [
        'config',
        'container',
        'control-node',
        'server-manager',
        'utils',
    ]

include = ['#/controller/src', '#/build/include', '#src/contrail-common', '#controller/lib']

libpath = ['#/build/lib']

libs = ['boost_system', 'boost_thread', 'log4cplus']
if platform.system() == 'Windows':
    libs.append('windowsstubs')
else:
    libs.append('pthread')

common = DefaultEnvironment().Clone()
if common['OPT'] == 'production':
    libs.append('tbb')
else:
    libs.append('tbb_debug')

common.Append(LIBPATH = libpath)
common.Prepend(LIBS = libs)

if platform.system() != 'Windows':
    common.Append(CXXFLAGS = '-Wall -Werror -Wsign-compare')

    if subprocess.check_output("g++ --version | grep g++ | awk '{print $3}'",
                               shell=True).rstrip() == "4.8.5":
        common.Append(CCFLAGS = '-Wno-narrowing -Wno-conversion-null')

if platform.system() == 'Linux':
    if not platform.linux_distribution()[0].startswith('XenServer'):
        common.Append(CCFLAGS = ['-Wno-unused-local-typedefs'])
elif platform.system() == 'FreeBSD':
    common.Append(CCFLAGS = ['-Wno-unused-local-typedefs'])
common.Append(CPPPATH = include)
common.Append(CCFLAGS = [common['CPPDEFPREFIX'] + 'RAPIDJSON_NAMESPACE=contrail_rapidjson'])

BuildEnv = common.Clone()

if platform.system() == 'Linux':
    BuildEnv.Append(CCFLAGS = ['-DLINUX'])
elif platform.system() == 'Darwin':
    BuildEnv.Append(CCFLAGS = ['-DDARWIN'])
elif platform.system() == 'FreeBSD':
    BuildEnv.Prepend(LINKFLAGS = ['-lprocstat'])

#
# Message documentation for common modules
#

# base
BuildEnv['BASE_DOC_FILES'] = contrail_common_base_doc_files

# IO
BuildEnv['IO_DOC_FILES'] = contrail_common_io_doc_files

# DB
db_doc_files = []
db_doc_target = BuildEnv['TOP'] + '/db/'
db_doc_files += BuildEnv.SandeshGenDoc('#controller/src/db/db.sandesh', db_doc_target)
BuildEnv['DB_DOC_FILES'] = db_doc_files

# sandesh
controller_vns_sandesh_doc_files = []
sandesh_doc_target = BuildEnv['TOP'] + '/sandesh/common/'
controller_vns_sandesh_doc_files += BuildEnv.SandeshGenDoc('#controller/src/sandesh/common/vns.sandesh', sandesh_doc_target)
BuildEnv['SANDESH_DOC_FILES'] = controller_vns_sandesh_doc_files
Export('controller_vns_sandesh_doc_files')

# Ksync
ksync_doc_files = []
ksync_doc_target = BuildEnv['TOP'] + '/ksync/'
ksync_doc_files += BuildEnv.SandeshGenDoc('#controller/src/ksync/ksync.sandesh', ksync_doc_target)
BuildEnv['KSYNC_DOC_FILES'] = ksync_doc_files

# BGP
bgp_doc_files = []
bgp_doc_target = BuildEnv['TOP'] + '/bgp/'
bgp_doc_files += BuildEnv.SandeshGenDoc('#controller/src/bgp/bgp_log.sandesh', bgp_doc_target)
bgp_doc_files += BuildEnv.SandeshGenDoc('#controller/src/bgp/routing-instance/route_aggregate_internal.sandesh', bgp_doc_target)
bgp_doc_files += BuildEnv.SandeshGenDoc('#controller/src/bgp/routing-instance/route_aggregate.sandesh', bgp_doc_target)
bgp_doc_files += BuildEnv.SandeshGenDoc('#controller/src/bgp/routing-instance/routing_instance_analytics.sandesh', bgp_doc_target)
bgp_doc_files += BuildEnv.SandeshGenDoc('#controller/src/bgp/routing-instance/routing_table.sandesh', bgp_doc_target)
bgp_doc_files += BuildEnv.SandeshGenDoc('#controller/src/bgp/routing-instance/service_chaining_internal.sandesh', bgp_doc_target)
bgp_doc_files += BuildEnv.SandeshGenDoc('#controller/src/bgp/routing-instance/service_chaining.sandesh', bgp_doc_target)
bgp_doc_files += BuildEnv.SandeshGenDoc('#controller/src/bgp/routing-instance/static_route_internal.sandesh', bgp_doc_target)
bgp_doc_files += BuildEnv.SandeshGenDoc('#controller/src/bgp/routing-instance/static_route.sandesh', bgp_doc_target)
bgp_doc_files += BuildEnv.SandeshGenDoc('#controller/src/bgp/bgp_peer_internal.sandesh', bgp_doc_target)
bgp_doc_files += BuildEnv.SandeshGenDoc('#controller/src/bgp/virtual_network.sandesh', bgp_doc_target)
bgp_doc_files += BuildEnv.SandeshGenDoc('#controller/src/bgp/peer_info.sandesh', bgp_doc_target)
bgp_doc_files += BuildEnv.SandeshGenDoc('#controller/src/bgp/vrouter.sandesh', bgp_doc_target)
bgp_doc_files += BuildEnv.SandeshGenDoc('#controller/src/bgp/bgp_peer.sandesh', bgp_doc_target)
BuildEnv['BGP_DOC_FILES'] = bgp_doc_files

# XMPP
xmpp_doc_files = []
xmpp_doc_target = BuildEnv['TOP'] + '/xmpp/sandesh/'
xmpp_doc_files += BuildEnv.SandeshGenDoc('#controller/src/xmpp/sandesh/xmpp_peer_info.sandesh', xmpp_doc_target)
xmpp_doc_files += BuildEnv.SandeshGenDoc('#controller/src/xmpp/sandesh/xmpp_client_server_sandesh.sandesh', xmpp_doc_target)
xmpp_doc_files += BuildEnv.SandeshGenDoc('#controller/src/xmpp/sandesh/xmpp_trace_sandesh.sandesh', xmpp_doc_target)
xmpp_doc_files += BuildEnv.SandeshGenDoc('#controller/src/xmpp/sandesh/xmpp_state_machine_sandesh.sandesh', xmpp_doc_target)
xmpp_doc_files += BuildEnv.SandeshGenDoc('#controller/src/xmpp/sandesh/xmpp_server.sandesh', xmpp_doc_target)
xmpp_doc_files += BuildEnv.SandeshGenDoc('#controller/src/xmpp/sandesh/xmpp_message_sandesh.sandesh', xmpp_doc_target)
BuildEnv['XMPP_DOC_FILES'] = xmpp_doc_files

# IFMAP server
ifmap_server_doc_files = []
ifmap_server_doc_target = BuildEnv['TOP'] + '/ifmap/'
ifmap_server_doc_files += BuildEnv.SandeshGenDoc('#controller/src/ifmap/ifmap_log.sandesh', ifmap_server_doc_target)
ifmap_server_doc_files += BuildEnv.SandeshGenDoc('#controller/src/ifmap/ifmap_server_show_internal.sandesh', ifmap_server_doc_target)
ifmap_server_doc_files += BuildEnv.SandeshGenDoc('#controller/src/ifmap/ifmap_server_show.sandesh', ifmap_server_doc_target)
BuildEnv['IFMAP_SERVER_DOC_FILES'] = ifmap_server_doc_files

# IFMAP client
ifmap_client_doc_files = []
ifmap_client_doc_target = BuildEnv['TOP'] + '/ifmap/'
ifmap_client_doc_files += BuildEnv.SandeshGenDoc('#controller/src/ifmap/ifmap_agent.sandesh', ifmap_client_doc_target)
BuildEnv['IFMAP_CLIENT_DOC_FILES'] = ifmap_client_doc_files


BuildEnv['INSTALL_DOC_PKG'] = BuildEnv['INSTALL_DOC'] + '/contrail-docs/html'
BuildEnv['INSTALL_MESSAGE_DOC'] = BuildEnv['INSTALL_DOC_PKG'] + '/messages'

BuildEnv.SConscript(dirs=['vnsw'], exports='BuildEnv')

for dir in subdirs:
    BuildEnv.SConscript(dir + '/SConscript',
                         exports='BuildEnv',
                         variant_dir=BuildEnv['TOP'] + '/' + dir,
                         duplicate=0)
